/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Acessar.java
 *
 * Created on 22/05/2011, 13:32:38
 */
package pitaqueiro;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lucas
 */
public class Acessar extends javax.swing.JFrame {
    
    String Algoritmo,NomeUsuario;
    PerfilUsuario usuario;
    int IndiceComecaDe1 ,IntNumRecomendacoes,IndiceUsuario;
    /** Creates new form Acessar */
    public Acessar() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TipoDoAlgoritmo = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        List<String> nomeUsuario = new ArrayList<String>();
        ManipulacaoDeArquivo leArq = new ManipulacaoDeArquivo();

        try{
            leArq.leEstabelecimentos();
            leArq.leOpinioes();
            List<Estabelecimento> listuaEstabelecimento = leArq.getListaEstabelecimento();
            for(PerfilUsuario use: leArq.getListaPerfilUsuarioSistema()){
                nomeUsuario.add(use.getNome());
            }
        }catch (Exception ex){
            ex.getMessage();
        }
        Usuarios = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        BotaoPopularidade = new javax.swing.JCheckBox();
        BotaoPersonalizada = new javax.swing.JCheckBox();
        BotaoGerarRecomendacoes = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        List<String> qntRecomendacoes = new ArrayList<String>();
        LeituraDeArquivo leArq2 = new LeituraDeArquivo();
        List<Estabelecimento> listaEstabelecimento2=null;
        int ContaRecomendacoes=0;
        String strSaida=null;
        try{
            leArq2.leEstabelecimentos();
            listaEstabelecimento2 = leArq2.getListaEstabelecimento();
        }catch (Exception ex){
            ex.getMessage();
        }
        for(int i=1 ;i<=listaEstabelecimento2.size();i++){
            strSaida = "Receber ";
            ContaRecomendacoes +=1;
            strSaida += ContaRecomendacoes;
            qntRecomendacoes.add(strSaida);
            strSaida = null;

        }
        QntRecomendacoes = new javax.swing.JComboBox();
        BotaoVoltar = new javax.swing.JButton();
        PainelTabela = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        //Erro o  intNumRecomendacoes sempre esta sendo zero, nao entra no for

        //List<String[]> Itens = new ArrayList<String[]>();
        //List<String> linha = new ArrayList<String>();
        //ManipulacaoDeArquivo manipulaArq = new ManipulacaoDeArquivo();
        String [] cabesalho = {"Estanelecimento","Avaliacao global"};
        String matriz[][]=null;
        //try{
            //manipulaArq.leEstabelecimentos();
            //manipulaArq.leOpinioes();
            //List<Estabelecimento> listaEstabelecimento = leArq.getListaEstabelecimento();
            matriz = new String[5/*deve ser o numero de recomendacoes escolhidas*/][2];
            //
            for(int i = 0; i < 5/*deve ser o numero de recomendacoes escolhidas*/;i++){
                //    matriz[i][0] =manipulaArq.geraRankingRestaurantes().get(i).split(";")[0];
                //    matriz[i][1] =manipulaArq.geraRankingRestaurantes().get(i).split(";")[1];
                matriz[i][0]=null;
                matriz[i][1]=null;
            }
            //}catch (Exception ex){
            //    ex.getMessage();
            //}
        Tabela = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 13));
        jLabel1.setText("Usuario: ");

        Usuarios.setModel(new javax.swing.DefaultComboBoxModel(nomeUsuario.toArray()));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel4.setText("Tipo de recomendação:");

        BotaoPopularidade.setText("Por popularidade");
        TipoDoAlgoritmo.add(BotaoPopularidade);
        BotaoPopularidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoPopularidadeActionPerformed(evt);
            }
        });

        BotaoPersonalizada.setText("Personalizada");
        TipoDoAlgoritmo.add(BotaoPersonalizada);
        BotaoPersonalizada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoPersonalizadaActionPerformed(evt);
            }
        });

        BotaoGerarRecomendacoes.setText("Gerar recomendações");
        BotaoGerarRecomendacoes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoGerarRecomendacoesActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Recomendações: ");

        QntRecomendacoes.setFont(new java.awt.Font("Tahoma", 0, 12));
        QntRecomendacoes.setModel(new javax.swing.DefaultComboBoxModel(qntRecomendacoes.toArray()));
        QntRecomendacoes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                QntRecomendacoesActionPerformed(evt);
            }
        });

        BotaoVoltar.setText("Voltar");
        BotaoVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoVoltarActionPerformed(evt);
            }
        });

        PainelTabela.setVisible(false);

        Tabela.setModel((new javax.swing.table.DefaultTableModel(
            matriz,cabesalho) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        }));
        jScrollPane1.setViewportView(Tabela);

        javax.swing.GroupLayout PainelTabelaLayout = new javax.swing.GroupLayout(PainelTabela);
        PainelTabela.setLayout(PainelTabelaLayout);
        PainelTabelaLayout.setHorizontalGroup(
            PainelTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PainelTabelaLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        PainelTabelaLayout.setVerticalGroup(
            PainelTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PainelTabelaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(BotaoPopularidade)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(BotaoPersonalizada)))
                .addContainerGap(298, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(QntRecomendacoes, 0, 256, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Usuarios, 0, 292, Short.MAX_VALUE)))
                .addGap(134, 134, 134))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(213, Short.MAX_VALUE)
                .addComponent(BotaoVoltar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BotaoGerarRecomendacoes)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addComponent(PainelTabela, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(71, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {BotaoGerarRecomendacoes, BotaoVoltar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(Usuarios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(QntRecomendacoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BotaoPersonalizada)
                    .addComponent(BotaoPopularidade))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(PainelTabela, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 116, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BotaoGerarRecomendacoes)
                    .addComponent(BotaoVoltar))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {BotaoGerarRecomendacoes, BotaoVoltar});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BotaoGerarRecomendacoesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoGerarRecomendacoesActionPerformed
     
    if(Algoritmo==null){
       new EscolhaAlgoritmoERRO().show();
    }else if(Algoritmo.toUpperCase().equals("POPULARIDADE")||Algoritmo.toUpperCase().equals("PERSONALIZADO")){
            //Quantidade de recomendacoes
            IndiceComecaDe1 = 1;
            IntNumRecomendacoes =IndiceComecaDe1+ QntRecomendacoes.getSelectedIndex();
            
            //Nome do usaurio
            IndiceUsuario = Usuarios.getSelectedIndex();
            NomeUsuario = DarNomeUsuario(IndiceUsuario);
            
            //Objeto usuario
            usuario  = RetornaUsuario(IndiceUsuario);
            
            
       /*
        * Desenha a tabela
        */
            
            List<String> ListaTeste;
            ManipulacaoDeArquivo manipulaArq = new ManipulacaoDeArquivo();
            
            String [] cabesalho = {"Estanelecimento","Avaliacao global"};
            String matriz[][]=null;
            try{
            manipulaArq.leEstabelecimentos();
            manipulaArq.leOpinioes();
            List<Estabelecimento> listaEstabelecimento = manipulaArq.getListaEstabelecimento();
            matriz = new String[IntNumRecomendacoes][2];
            manipulaArq.geraRankingRestaurantes();
            for(int i = 0; i < IntNumRecomendacoes;i++){
                if(Algoritmo.equals("POPULARIDADE")){
                    matriz[i][0] =manipulaArq.getListaRestaurantePopular().get(i).split(";")[0];
                    matriz[i][1] =manipulaArq.getListaRestaurantePopular().get(i).split(";")[1];
                }
                else if(Algoritmo.toUpperCase().equals("PERSONALIZADO")){
                   
                   
                    matriz[i][0] =manipulaArq.getRecomendacoesRestaurantes(usuario, manipulaArq).get(i);
                    matriz[i][1] =null;
                }

            }
            }catch (Exception ex){
                System.out.print(ex.getMessage());
                ex.getMessage();
            }
            Tabela = new javax.swing.JTable();

            Tabela.setModel((new javax.swing.table.DefaultTableModel(
                matriz,cabesalho) {
                boolean[] canEdit = new boolean [] {
                    false, false, false, false
                };

                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit [columnIndex];
                }
            }));

            jScrollPane1.setViewportView(Tabela);

            PainelTabela.setVisible(true);
    }
        
    }//GEN-LAST:event_BotaoGerarRecomendacoesActionPerformed

    private void BotaoPopularidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoPopularidadeActionPerformed
        Algoritmo = "POPULARIDADE";
    }//GEN-LAST:event_BotaoPopularidadeActionPerformed

    private void BotaoPersonalizadaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoPersonalizadaActionPerformed
        Algoritmo = "PERSONALIZADO";
    }//GEN-LAST:event_BotaoPersonalizadaActionPerformed

    private void QntRecomendacoesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_QntRecomendacoesActionPerformed
        
}//GEN-LAST:event_QntRecomendacoesActionPerformed

    private void BotaoVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoVoltarActionPerformed
        new Menu().show();
        this.dispose();
    }//GEN-LAST:event_BotaoVoltarActionPerformed
    
    /*
     * Retorna o nome do usuario
     * @param int
     * @return String
     */
    public String DarNomeUsuario(int indice) {
        String nome=null;
        ManipulacaoDeArquivo manipula2 = new ManipulacaoDeArquivo();
        try {
            manipula2.leEstabelecimentos();
        } catch (IOException ex) {
            Logger.getLogger(ExibirPerfil.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            manipula2.leOpinioes();
        } catch (IOException ex) {
            Logger.getLogger(ExibirPerfil.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (int i=0;i<manipula2.getListaPerfilUsuarioSistema().size();i++){
           
            if(i==indice){
                nome=manipula2.getListaPerfilUsuarioSistema().get(i).getNome();
                
            }
                   
        }
        return nome;
        
    }
    
    
    /*
     * retorna o usuario
     */
    public PerfilUsuario RetornaUsuario(int indice) {
        PerfilUsuario usuario=null;
        ManipulacaoDeArquivo manipula2 = new ManipulacaoDeArquivo();
        try {
            manipula2.leEstabelecimentos();
        } catch (IOException ex) {
            Logger.getLogger(ExibirPerfil.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            manipula2.leOpinioes();
        } catch (IOException ex) {
            Logger.getLogger(ExibirPerfil.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (int i=0;i<manipula2.getListaPerfilUsuarioSistema().size();i++){
           
            if(i==indice){
                usuario=manipula2.getListaPerfilUsuarioSistema().get(i);
                
            }
                   
        }
        return usuario;
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Acessar().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotaoGerarRecomendacoes;
    private javax.swing.JCheckBox BotaoPersonalizada;
    private javax.swing.JCheckBox BotaoPopularidade;
    private javax.swing.JButton BotaoVoltar;
    private javax.swing.JPanel PainelTabela;
    private javax.swing.JComboBox QntRecomendacoes;
    private javax.swing.JTable Tabela;
    private javax.swing.ButtonGroup TipoDoAlgoritmo;
    private javax.swing.JComboBox Usuarios;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
